(function(){
    function SetUserInfo(){
        this.init(); 
    }

    SetUserInfo.prototype = {
        constructor:SetUserInfo,
        init:function(){
            this.nodata = '<tr><td  colspan="5">暂无信息</td></tr>';
            var _this = this;
        
                this.getReportList(1);
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getReportList(1);
                });

                $.extend({page:function(pn){
                    _this.getReportList(pn);
                }});
                // 添加用户
                this.addNewUser();

             




        },
        getReportList:function( pn ){
            var _this = this;
            var searchName = $("#username").val();
            if( searchName )
            {
                searchName = "&username="+searchName;
            }
            $.ajax({
                type:"get",
                url:"/admin/admin_users?ps=20&pn="+pn+searchName,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        var pageno = count.pn;
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var username  = value.username ;
                                var password = value.password;
                                var auth = value.auth;
                             
                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td>'+((pageno-1)*20+key+1)+'</td>\
                                                <td>'+username+'</td>\
                                                <td>'+password+'</td>\
                                                <td>'+auth+'</td>\
                                                <td></td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                          
                            
                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);



                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        addNewUser:function(){
            var _this = this;
            $("#addNewUser").click(function(){
                $("#addUserModal").modal();
            });
            $("#addUserBtn").unbind().click(function(){
                _this.doAddUser();

            });

        },
        doAddUser:function(){
            var _this = this;
            var newName = $("#addusername").val();
            var newpwd = $("#adduserpwd").val();
            if( !newName )
            { 
                $("#addusername").focus();
                return false;
            }
            if( !newpwd )
            { 
                $("#adduserpwd").focus();
                return false;
            }
            $.ajax({
                type:"get",
                url:"/admin/add_admin_user?username="+newName+"&password="+newpwd,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#addUserBtn").popover("show");
                        $("#addusername,#adduserpwd").val("");
                        _this.getReportList(1);
                        setTimeout(function(){
                           $("#addUserBtn").popover("hide");
                           $("#addUserModal").modal("hide");
                       }, 800);
                    }
                    else
                    {
                         $("#addUserModal").modal("hide");
                             if( code == 2 )
                             {
                               
                                $("#tipsContent").html("你还未登录");
                                $("#loginoutModal").modal();
                                setTimeout(function(){
                                    location.href = "login.html";
                                }, 800);
                             }
                             else
                             {
                                $("#tipsContent").html(desc);
                                $("#loginoutModal").modal();

                             }
                    }

                }
            });
        }
  
   
      
      
    };





  new SetUserInfo();

}());